package com.example.administrator.personalmail.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.administrator.personalmail.MyApplication;
import com.example.administrator.personalmail.R;
import com.example.administrator.personalmail.adapter.AddressAdapter;
import com.example.administrator.personalmail.api.Contants;
import com.example.administrator.personalmail.greendao.AddressDao;
import com.example.administrator.personalmail.greendao.DaoMaster;
import com.example.administrator.personalmail.model.Address;
import com.example.administrator.personalmail.widget.GreenDaoManager;

import java.util.List;

public class AddressActivity extends AppCompatActivity {
    private Button addNewAddress;
    private RecyclerView mRecyclerView;
    private AddressAdapter mAddressAdapter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_list);
        init();

    }

    private void init(){
        mRecyclerView = findViewById(R.id.recycler_view_address);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(AddressActivity.this));
        DaoMaster.DevOpenHelper devOpenHelper = new
                DaoMaster.DevOpenHelper(MyApplication.getContext(), "address-db", null);
        List<Address> addresses = new DaoMaster(devOpenHelper.getReadableDb()).newSession().loadAll(Address.class);
        if (addresses != null && addresses.size()>0){
            mAddressAdapter = new AddressAdapter(addresses,AddressActivity.this);
            mRecyclerView.setAdapter(mAddressAdapter);
            mAddressAdapter.setEditAddressListenner(new AddressAdapter.EditAddressListenner() {
                @Override
                public void onClick(Address address) {
                    Intent intent = new Intent(AddressActivity.this,AddNewAddressActivity.class);
                    intent.putExtra("editData",address);
                    startActivityForResult(intent,1);
                }
            });
        }

        addNewAddress = findViewById(R.id.btn_add_new_address);
        addNewAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(AddressActivity.this,AddNewAddressActivity.class);
                startActivityForResult(intent,1);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Contants.REQUEST_SUCCESS){
            List<Address> addresses =  getAddressDao().loadAll();
//            Toast.makeText(AddressActivity.this,addresses.get(0).getAddress(),Toast.LENGTH_SHORT).show();
            mAddressAdapter = new AddressAdapter(addresses,AddressActivity.this);
            mRecyclerView.setAdapter(mAddressAdapter);
            mAddressAdapter.setEditAddressListenner(new AddressAdapter.EditAddressListenner() {
                @Override
                public void onClick(Address address) {
                    Intent intent = new Intent(AddressActivity.this,AddNewAddressActivity.class);
                    intent.putExtra("editData",address);
                    startActivityForResult(intent,1);
                }
            });
        }else if(resultCode == Contants.EDIT_ADDRESS_SUCCESS){
            List<Address> addresses =  getAddressDao().loadAll();
//            Toast.makeText(AddressActivity.this,addresses.get(0).getAddress(),Toast.LENGTH_SHORT).show();
            mAddressAdapter = new AddressAdapter(addresses,AddressActivity.this);
            mRecyclerView.setAdapter(mAddressAdapter);

        }
    }

    public AddressDao getAddressDao()
    {
        return GreenDaoManager.getInstance().getSession().getAddressDao();
    }
}
